#coding=utf8
import huayiQDB.controller.huayiUtils as hyu
import scrapy
import huayiQDB.controller.log as log

logger = log.logger

def assemble_listbox_01_UrlId(pattern_list, str):
    """
    listbox_01的内容id，title
    :param pattern_list:
    :param str:
    :return: listbox_01_info {     #dict about listbox_01_info
        listbox_01_id
        listbox_01_title
    }
    """
    listbox_01_info = {}
    listbox_01_dept_id_list = []
    listbox_01_title_list = []
    listbox_01_tmp = hyu.selectInfo(pattern_list[6], str)
    for single_tmp in listbox_01_tmp:
        listbox_01_dept_id_list = hyu.selectInfo(pattern_list[5], str)
        listbox_01_title_list.append("".join(hyu.selectInfo(pattern_list[3], single_tmp)))
    listbox_01_info['istbox_01_dept_id'] = listbox_01_dept_id_list
    listbox_01_info['listbox_01_title'] = listbox_01_title_list
    return listbox_01_info

def assemble_menubox_03_UrlId(pattern_list, str):
    """
    深度遍历，menubox_01内的内容
    :param pattern_list: 正则公式
    :param str: 源
    :return: menubox_01_info结构信息字典类型
    menubox_01_info={
        menubox_03_dept_id:str
        menubox_03_title:str
        menubox_01_list = [
            {
                menubox_01_list
            }...
        ]
    }
    """
    menubox_01_info = {'menubox_03_dept_id': "".join(hyu.selectInfo(pattern_list[1], str))}
    title_tmp = "".join(hyu.selectInfo(pattern_list[2], str))  #筛选过渡title/id
    menubox_01_info['menubox_03_title'] = "".join(hyu.selectInfo(pattern_list[3], title_tmp))
    menubox_01_info['menubox_03_idlist'] = "".join(hyu.selectInfo(pattern_list[4], str)) #menubox_03内的list汇总  list类型
    menubox_01_info['menubox_01_list'] = assemble_listbox_01_UrlId(pattern_list,str) #menubox_01内的详细list类型
    return menubox_01_info

def deepAssembleUrlId(pattern_list,str):
    """
    处理response响应格式，筛选URL_ID，组装URL_ID字典
    :param pattern_list: 正则公式list
    :param str: 处理前的响应
    :return: 组装后地URLlist字典的列表
    detail:[
            dict{
                    menubox_03_dept_id:menubox_02`s dept_id
                    menubox_03_title:menubox_02`s title
                    menubox_03_idlist:[menubox_01,...]
                    menubox_01_list:[{
                        istbox_01_dept_id:[id,...],
                        listbox_01_title:[name,...]
                    }...]
                }
            ]
    """
    urllist = []
    str_ass = hyu.tansform_respone(str) #respose数据
    pattern0 = hyu.selectInfo(pattern_list[0], str_ass)
    for tmp in pattern0:
        urllist.append(assemble_menubox_03_UrlId(pattern_list, tmp))
    return urllist

# 单个课程信息说明：
# item_course{
#         item_name:课程名字
#         item_txts:学分
#         item_url: 课程链接
#         item_man:负责人
#         item_unit:单位
#         item_number:项目编号
#         item_attention:关注度
# }



def collect_OneCourseInfo(pattern_list, response_text):
    """
    收集单个课程的信息
    :param pattern_list:
    :param response_text:
    :return:
    """
    onecourse = {}
    #onecourse['item_attention'] = "".join(hyu.selectInfo(pattern_list[1], response_text)).strip()
    onecourse['item_url'] = "".join(hyu.selectInfo(pattern_list[2], response_text)).strip()
    onecourse['item_name'] = "".join(hyu.selectInfo(pattern_list[3], response_text)).strip()
    txts = hyu.selectInfo(pattern_list[4], "".join(response_text)) #拆分四组不同的信息
    onecourse['item_txts'] = txts[0].strip()   #国家I类 5.0学分
    onecourse['item_number'] = "".join(hyu.selectInfo(pattern_list[5], "".join(txts[1]))).strip() #2020-17-00-206 (国)
    onecourse['item_man'] = "".join(hyu.selectInfo(pattern_list[5], "".join(txts[2]))).strip()   #艾智华
    onecourse['item_unit'] = "".join(hyu.selectInfo(pattern_list[6], "".join(txts[3]))).strip()  #中国人民解放军西部战区总医院
    return onecourse

def collectCourseInfo_Num(pattern_list,response_text):
    """
    获取课程的数量
    :param pattern_list: pattern_list
    :param response_text: response_text
    :return: int
    """
    pattern = r'<span id="labAll">(.*?)</span>'
    num = int("".join(hyu.selectInfo(pattern, response_text)))
    if num and num < 0 :
        raise ValueError("课程的数量获取错误")
    return num

def collectCoursePage_VIEWSTATE(pattern, response_text):
    return "".join(hyu.selectInfo(pattern, response_text)[0])

def collectCoursePage_VIEWSTATEGENERATOR(pattern,response_text):
    return "".join(hyu.selectInfo(pattern, response_text)[0])

def collect_OnePageCourseInfo(pattern_list, response_text):
    """
    收集一页中的课程的信息
    :param pattern_list:
    :param response_text:
    :return: list
    """
    OnePageCourseInfo = []
    text = hyu.tansform_respone(response_text)
    level0 = hyu.selectInfo(pattern_list[0], text)  # 拆分当页<div class="item_r">...</div>
    for level0_tmp in level0:
        OnePageCourseInfo.append(collect_OneCourseInfo(pattern_list, level0_tmp))
    return OnePageCourseInfo



# def collect_AllCourseInfo(pattern_list, response_text):
#     """
#     通过不停访问下一页，收集一个科目的全部课程的信息
#     :param courseInfo_list:
#     :param pattern_list:
#     :param response_text:
#     :return: list
#     """
#     pass



def course_detail_list(pattern_course_list, str1):
    """
    进入单个课程内的学习项目
    :param pattern_list:
    :param str:
    :return:{
        study_url:学习链接
        study_name:学习名称
        study_teacher:授课老师
        study_unit:单位
        study_state:学习状态
    }
    """
    course_study_list = {}
    # logger.info('str----------:'+str1)
    course_study_list['study_value'] = "".join(hyu.selectInfo(pattern_course_list[6], str1)[0]).strip()
    course_study_list['study_url'] = "".join(hyu.selectInfo(pattern_course_list[1], str1)).strip()
    course_study_list['study_name'] = "".join(hyu.selectInfo(pattern_course_list[2], str1)).strip()
    # logger.info('study_value--------------:' + course_study_list['study_value'])
    course_study_tmp = hyu.selectInfo(pattern_course_list[3], str1)
    #course_study_list['study_state'] = "".join(hyu.selectInfo(pattern_course_list[5], "".join(course_study_tmp)))
    course_study_list['study_teacher'] = "".join(hyu.selectInfo(pattern_course_list[4], "".join(course_study_tmp[0]))).strip()
    course_study_list['study_unit'] = "".join(hyu.selectInfo(pattern_course_list[4], "".join(course_study_tmp[1]))).strip()
    return course_study_list

def study_detail_AllList(pattern_course_list, response_text):
    """
    收集单个课程下的学习情况
    :param pattern_course_list:
    :param str:
    :return:
    """
    course_detail_alllist = []
    text = hyu.tansform_respone(response_text)
    level0 = hyu.selectInfo(pattern_course_list[0], text)  # 拆分当页<div class="item_r">...</div
    for level0_tmp in level0:
        course_detail_alllist.append(course_detail_list(pattern_course_list, level0_tmp))
    return course_detail_alllist

def study_relation_id(pattern,response_text):
    return "".join(hyu.selectInfo(pattern, response_text))

def study_user_id(pattern,response_text):
    return "".join(hyu.selectInfo(pattern, response_text))

def question_detail_list(pattern_course_list, strs, question_type):
    """
    筛出单个选择题信息
    :param pattern_course_list:
    :param str:
    :return:  question_detail{
        question_id:
        question_name:
        question_answer:
        question_choiceValue:[]
        question_choiceName:[]
    }
    """
    question_detail_list = {}
    question_detail_list['q_name'] = "".join(hyu.selectInfo(pattern_course_list[1], strs)).strip() # question_topic_content
    question_detail_list['q_IDValue'] = "".join(hyu.selectInfo(pattern_course_list[2], strs)).strip() # questionID_value
    question_detail_list['result_value'] = "".join(hyu.selectInfo(pattern_course_list[3], strs)).strip() # result_value
    question_detail_list['q_nameid'] = hyu.selectInfo(pattern_course_list[4], strs)  # 待选答案的value标记列表
    question_detail_list['q_choiceValueList'] = hyu.selectInfo(pattern_course_list[5], "".join(strs)) # 待选答案的content内容列表
    question_detail_list['a_name_all'] = ArrayToStr(hyu.selectInfo(pattern_course_list[5],
                                                               strs)) # 待选答案的content内容列表
    question_detail_list['q_choiceNameList'] = hyu.selectInfo(pattern_course_list[6], "".join(strs)) #待选答案的name标记列表
    result_question_tmp = hyu.selectInfo(pattern_course_list[7], strs)
    question_detail_list['q_resultName'] = "".join(hyu.selectInfo(pattern_course_list[8], result_question_tmp[0])).strip()  #result_name
    question_detail_list['q_IDName'] = "".join(hyu.selectInfo(pattern_course_list[9], result_question_tmp[1])).strip()  #questionID_name
    question_detail_list['q_question_test'] = '0' #从0开始
    question_detail_list['q_question_correct'] = ''
    question_detail_list['question_type'] = question_type
    question_detail_list['q_question_correct_name'] = ''
    # logger.info("考试题信息：")
    # for key, value in question_detail_list.items():
    #     if isinstance(value, str):
    #         logger.info(key+":"+value)
    #     elif isinstance(value, list):
    #         list_str = ",".join(str(i) for i in value)
    #         logger.info(key + ":" + list_str)
    return question_detail_list


def ArrayToStr(list):
    nums = len(list)
    str_list = []
    if nums:
        for i, one in enumerate(list):
            if i < nums-1:
                str_list.append(str(one))
                str_list.append(";")
            else:
                str_list.append(str(one))
    else:
        str_list.append("nums数量为零")
    return "".join(str_list)

def question_detail_AllList(response,question_type): #得到一页中的题
    """
    筛选出一页选择题数组
    :param pattern_course_list:
    :param str:
    :return:
    """

    pattern = [
        '<tr align="left">(.*?)</td>		</tr>',  # 0筛出单个问题数组
        '<span id="gvQuestion_question_[0-9]">(.*?)</span>',  # question_topic_content
        'id="gvQuestion_question_id_[0-9]" value="(.*?)"',  # questionID_value
        'id="gvQuestion_result_[0-9]" value="(.*?)"',  # result_value
        'name="gvQuestion\$ctl0[0-9]\$rbl" value="(.*?)"',  # 待选答案的value标记列表
        '<label for="gvQuestion_rbl_[0-9]_[0-9]_[0-9]">(.*?)</label>',  # 待选答案的content内容列表
        '<td><input id="gvQuestion_rbl_[0-9]_[0-9]_[0-9]" type="radio" name="(.*?)"',  # 待选答案的name标记列表
        '<input type="hidden"(.*?)/>',#拆分出result、question_id
        'name="(.*?)" id="gvQuestion_result_[0-9]"',  # result_name
        'name="(.*?)" id="gvQuestion_question_id_[0-9]"'  # questionID_name
    ]

    questionInfo = []
    NS_Info = response.meta['NS_Info']
    text = hyu.tansform_respone(response.text)
    level0 = hyu.selectInfo(pattern[0], text)  # 拆分当页<div class="item_r">...</div>
    for level0_tmp in level0:
        questionInfo.append(question_detail_list(pattern, level0_tmp, NS_Info['Item_Value']))
    return questionInfo


def collect_ErrorExamResult1(pattern, response_text, url_exam):
    """
    收集错误信息，返回dict类型
    :param pattern:
    :param response:
    :return:
    {
        err_num:错题数量
        err_content:[错题内容]
    }
    """
    questionInfo = {}
    state = str(hyu.selectInfo(pattern[3], response_text)[0]).strip()
    if state == '考试没过！':
        questionInfo['err_num'] = int(hyu.selectInfo_num(pattern[0], response_text)[0])
        contents = hyu.selectInfo(pattern[1], response_text)
        err_content_list = []
        for content in contents:
            err_content_list.append(content)
        questionInfo['err_content_list'] = err_content_list
    elif state == '考试通过！':
        questionInfo['err_num'] = 0

    #questionInfo['url_exam'] = "http://cme2.91huayi.com/pages/"+"".join(hyu.selectInfo(pattern[2], response_text))
    questionInfo['url_exam'] = url_exam
    # logger.info("错题数量:"+ str(questionInfo.get('err_num'))+",错题如下：")
    # for value in questionInfo.get('err_content_list'):
    #     logger.info(value)
    return questionInfo

def collect_ErrorExamResult2(pattern, response_text):
    """
    收集错误信息，返回dict类型
    :param pattern:
    :param response:
    :return:
    {
        err_num:错题数量
        err_content:[错题内容]
    }
    """
    questionInfo = {}
    state = str(hyu.selectInfo(pattern[3], response_text)[0]).strip()
    if state == '考试没过！':
        questionInfo['err_num'] = int(hyu.selectInfo_num(pattern[0], response_text)[0])
        contents = hyu.selectInfo(pattern[1], response_text)
        err_content_list = []
        for content in contents:
            err_content_list.append(content)
        questionInfo['err_content_list'] = err_content_list
    elif state == '考试通过！':
        questionInfo['err_num'] = 0
    questionInfo['url_exam'] = "http://cme2.91huayi.com/pages/"+"".join(hyu.selectInfo(pattern[2], response_text))
    # logger.info("错题数量:"+ str(questionInfo.get('err_num'))+",错题如下：")
    # for value in questionInfo.get('err_content_list'):
    #     logger.info(value)
    return questionInfo



#   part2
#   detail: 使用nav页面获取的各个课程请求ID，分别访问该ID并获取课程数量，通过课程数量分别按页面收集页面的信息，并访问当前页面的每一个课程
#   业务流程：
#       1.通过主爬虫parse中实现调度器函数，生成对每个分类的请求，对获取分类下的课程页面并处理得到该分类下的：“课程数量”，遍历访问该页下的课程
#       2.通过“课程数量”，分别访问各个页面，遍历访问该页下的课程，直至结束所有课程访问
#
#   关键方法要求说明：
#       遍历访问该页下的课程：访问单个课程，获得课程下的多个学习，访问单个学习
#       访问单个学习：进入学习页面，发送跳过视频连接并成功响应后，发送考试连接进入考试
#       考试：获取考试题，答题全选A，提交，返回结果页面，验证      在下载中间件内完成
#       验证：记录正确回答，并修改错误项的答案，继续答题，保证每次不能做全对，重复考试过程直至得到所有答案，将结果返回到Item Pipeline中保存到数据库


def selectAllCourseFromNav(pattern_Nav, response_text):
    """
    由knowledge_navigation.aspx页面获取各课程字典数据类型
    :param pattern_Nav: str
    :param response_text: str
    :return:课程集合 dict
    """
    text = hyu.tansform_respone(response_text)
    nav_list = "".join(hyu.selectInfo(pattern_Nav, text))
    nav_dict = hyu.transform_UrltoDict(nav_list)
    return nav_dict

def requestOneCourse(url,parse):
    yield scrapy.Request(url,callback=parse)

def nav_list(pattern, response_text):
    text = hyu.tansform_respone(response_text)
    search_condition = "".join(hyu.selectInfo(pattern[0], text))
    search_typeorscore_list = hyu.selectInfo(pattern[1], search_condition)
    search_type_list = hyu.selectInfo(pattern[2], search_typeorscore_list[0])
    class_list=[]
    for onelist in search_type_list:
        un_class_list = hyu.selectInfo(pattern[3], onelist)[0]
        temp_list = un_class_list.split('\",\"')
        oneclass = {str(temp_list[0]):str(temp_list[1])}
        oneclass[str(temp_list[2])] = str(temp_list[3])
        class_list.append(oneclass)
    return class_list